package com.tanhua.server.controller;

import com.tanhua.domain.db.User;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.server.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 处理请求的控制器类
 */
@RestController // @Controller + @ResponseBody
@RequestMapping("user")
@Slf4j
public class LoginController {

    @Autowired
    private UserService userService;


    /**
     * 请求路径：http://localhost:18080/user/findByMobile?mobile=18000110022
     */
    @GetMapping("findByMobile")
    public ResponseEntity<Object> findByMobile(String mobile){
        return userService.findByMobile(mobile);
    }

    /**
     * 添加
     * @RequestBody 请求的json自动转换为对象
     */
    @PostMapping
    public ResponseEntity<Object> save(@RequestBody User user){
        return userService.save(user);
    }


    /**
     * 接口名称：登录第一步---手机号登录
     * 接口路径：POST/user/login
     * 需求描述：发送验证码
     * 请求参数: {”phone“:"1234567"}
     */
    @PostMapping("login")
    public ResponseEntity<Object> login(@RequestBody Map<String,String> map){
        log.info("接口名称：登录第一步---手机号登录");
        String phone = map.get("phone");
        return userService.login(phone);
    }

    /**
     * 接口名称：登录第二步---验证码校验
     * 接口路径：POST/user/loginVerification
     */
    @PostMapping("loginVerification")
    public ResponseEntity<Object> loginVerification(@RequestBody Map<String,String> map){
        log.info("登录第二步---验证码校验");
        // 获取请求参数： 手机号码、验证码
        String phone = map.get("phone");
        String verificationCode	 = map.get("verificationCode");
        // 调用service实现登陆注册
        return userService.loginVerification(phone,verificationCode);
    }

}
